﻿using ReactiveUI;

namespace LS.Screening.UI.Common.Controls
{
    public class CommonUserControl<VM> : ReactiveUserControl<VM> where VM : ViewModel
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CommonUserControl()
        {
            var viewModel = ServiceHelper.GetService<VM>();
            this.DataContext = viewModel;
            this.ViewModel = viewModel;
            this.Loaded += UserControl_Loaded;
            this.Unloaded += UserControl_Unloaded;
        }

        /// <summary>
        /// UserControl加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            ViewModel?.Init();
        }

        /// <summary>
        /// UserControl卸载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Unloaded(object sender, System.Windows.RoutedEventArgs e)
        {
            ViewModel?.Unload();
        }
    }
}
